<?php
namespace Admin\Controller;
use Think\Controller;
class VideoController extends Controller {
	public function videolist(){
		$clubModel=M('club_video');
		$club=$clubModel->order('club_video_id')
		->join('club_video_type ON club_video.club_video_type = club_video_type.club_video_type_id' )
		->join('club_info ON club_video.club_id=club_info.club_id')->field('club_video_id,club_video_name,club_video_type_name,club_video_uptime,club_name,club_video_address')->select();
		//dump($club);
		$this->club=$club;
		$this->display();

	}

	public function addvideo(){
			$this->display();
	}
	public function do_addvideo(){
	     $data=$_POST;
		$file=$_FILES;
		$clubModel=M('club_video');
		$createresult=$clubModel->create();
		if($createresult){
			$clubModel->club_video_name=trim(I('post.club_video_name'));
			$clubModel->club_id=trim(I('post.club_id'));
			$clubModel->club_video_type=trim(I('post.club_video_type'));
			$clubModel->club_video_address=trim(I('post.club_video_address'));
			$clubModel->club_video_uptime=date("Y-m-d H:i:s");
			if($file['club_video_address']['error']==0){
				$info=$this->MyUpload();
				$clubModel->club_video_address=$info['club_video_address']['savename'];

			}else{
			$clubModel->club_video_address="nopic.jpg";
         
     }
    
      $addresult=$clubModel->add();

      if (false !== $addresult)
                 {
                    $this->success('添加成功！', '../Video/videolist');
                }else{ 
                   $this->error('数据错误'); 
               }


     } else{
            $this->error($clubModel->getError()); 	
	}


	}
	public function deleteClass(){
		$club_video_id=I('club_video_id');
		$clubModel=M('club_video');
		$club=$clubModel->where("club_video_id=$club_video_id");
		$result=$clubModel->where("club_video_id=$club_video_id")->delete();
		$picPath='Public/Uploads/video/'.$club[0]['club_video_address'];
		unlink($picPath);
		if($result){
			$this->success('删除成功');
		}else{
			$this->error('删除失败');
		}

	}
	public function editvideo(){
		$club_video_id=I('club_video_id');
		$clubModel=M('club_video');
		$club=$clubModel->where("club_video_id=$club_video_id")->select();

		//dump($club);
		$this->club=$club;
		$this->display();

	}
	public function updatevideo(){
		$data=$_POST;
		$file=$_FILES;
		dump($file);
		$club_video_id=$data['club_video_id'];
		dump($club_video_id);
		$clubModel=M('club_video');
		if($file['club_video_address']['size']==0){
			$result=$clubModel->where("club_video_id=$club_video_id")->save($data);
		}else{
			//修改头像
			$upload= new \Think\Upload();//实例化上传类
			$upload->maxSize = 31457280000000;
			$upload->allowExts  = array('mp4', 'rmvb', 'mkv', 'f4v','avi','3gp','swf','mpeg','rm','jpeg');// 设置附件上传类型
            $upload->rootPath  =  './';
            $upload->savePath =  'Public/Uploads/video/';// 设置附件上传目录
            $upload->autoSub = false;
            $upload->saveName = date("YmdHis").rand(100,999);//定义名字
            $info = $upload->upload();
    
            if(!$info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
             }else{// 上传成功 获取上传文件信息
				 //保存表单数据 包括附件数据
               	$data['club_video_address']= $info['club_video_address']['savename']; 
                $result=$clubModel->where("club_video_id=$club_video_id")->save($data);
                	if ($data['oldpic']!='nopic.jpg') {
                	$oldpicPath='Public/Uploads/'.$data['oldpic'];
                	unlink($oldpicPath);
               		 	}
            		}
        	}
        	//dump($data);
		if($result){
       		// $this->success('修改成功！','../Video/videolist');
     		 }
      		else{
        		//$this->error('修改失败！');
      			}
}



	    public function MyUpload(){
                //上传头像
              $upload = new \Think\Upload();// 实例化上传类
              $upload->maxSize  = 31457280000 ;// 设置附件上传大小
              $upload->allowExts  = array('mp4', 'rmvb', 'mkv', 'f4v','avi','3gp','swf','mpeg','rm');// 设置附件上传类型
              $upload->rootPath  =  './';
              $upload->savePath =  'Public/Uploads/video/';// 设置附件上传目录
              $upload->autoSub = false;
              $upload->saveName = date("YmdHis").rand(100,999);//定义名字
               // 上传单个文件 
              $info = $upload->upload();                    
                  if(!$info) {// 上传错误提示错误信息
                      $this->error($upload->getError());
                    }else{// 上传成功 
                        return $info;
                    }
                } 


}

?>